package com.wolf.fx;

import java.lang.reflect.InvocationTargetException;
import java.lang.reflect.Method;

public class FxController {


	
	public static void main(String[] args) {
		new FxController().init();
	}
	
	public void init(){
		//ServerRequestBean srb = new ServerRequestBean();
		cfb();
		Object[] ins = new Object[4];
		ins[0] = "add";
		ins[1] = 1200;
		ins[2] = 200;
		ins[3] = 300;
		System.out.println(getResult(ins));
	}
	
	public void cfb(){
		for(int i=0;i<10;i++){
			for(int j=0;j<10;j++){
				int z = i + j;
				Object[] it = new Object[4];
				it[0] = "add";
				it[1] = i;
				it[2] = j;
				it[3] = z;
				dataInputSjy(it);
			}
		}
//		for(int i=1;i<10;i++){
//			for(int j=1;j<10;j++){
//				int z = i * j;
//				Object[] it = new Object[4];
//				it[0] = "mul";
//				it[1] = i;
//				it[2] = j;
//				it[3] = z;
//				dataInputSjy(it);
//			}
//		}
		try {
			FxController fc = new FxController();
			Class clazz = fc.getClass();
			Method m = clazz.getDeclaredMethod("add_Gz", Object.class,Object.class);
			Object[] it = new Object[]{
				"add_Gz",fc,m
			};
			dataInputSjy(it);
		} catch (Exception e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}
	}

	public Object add_Gz(Object o1,Object o2){
		if(o1 == null || o2 == null){
			return null;
		}
		String os1 = o1+"";
		String os2 = o2+"";
		int os1Len = os1.length();
		int os2Len = os2.length();
		if(os1Len<os2Len){//替换
			String osm = os1;
			os1 = os2;
			os2 = osm;
			os1Len = os1.length();
			os2Len = os2.length();
		}
		os2 = String.format("%0"+os1Len+"d", Integer.parseInt(os2));
		String os1Array[] = stringToStringArray(os1);
		String os2Array[] = stringToStringArray(os2);
		String end = "";
		int m=0;//前一位
		int n=0;//最后一位
		for(int i=os1Len-1;i>=0;i--){//前一个数必须大于后一个数
			int a = Integer.parseInt(os1Array[i]);
			int b = Integer.parseInt(os2Array[i]);
			String c = getResult(new Object[]{"add",a,b})+"";
			String cArray[] = stringToStringArray(c);
			if(c.length() == 1){
				//当前最后一位加上上一次的前一位
				n = (Integer)getResult(new Object[]{"add",Integer.parseInt(c),m});
			}else{
				//当前最后一位加上上一次的前一位
				n = (Integer)getResult(new Object[]{"add",Integer.parseInt(cArray[1]),m});
				m = Integer.parseInt(cArray[0]);
			}
			end = n + end;
		}
		return Integer.parseInt(end);
	}
	
	private static int num = 5000;
	private static Object[][] sjy = new Object[num][];
	public void dataInputSjy(Object[] data){
		for(int i=0;i<num;i++){
			if(sjy[i] == null){
				sjy[i] = data;
				break;
			}
		}
	}
	
	public Object getResult(Object[] osd){
		if(osd == null){
			return null;
		}
		int osdLen = osd.length;
		if(osdLen<2){
			return null;
		}
		Object end = null;
		int endType=0;//结果的当前状态,用于置入应用规则
		for(int i=0;i<num;i++){
			Object[] os = sjy[i];//规则数据
			if(os != null){
				if(!os[0].equals(osd[0])){//先保证类型一致
					continue;
				}
				int osLen = os.length;
				if(osdLen<osLen-1){//传入数据比规则数据短，不再执行
					continue;
				}
				if(osdLen == osLen-1){
					endType = 1;
					for(int j=0;j<osdLen;j++){//传入数据和规则数据一样长，直接获取规则结果
						if(osd[j] != os[j] && !osd[j].equals(os[j])){
							break;
						}
						if(j == osLen-2){
							end = os[j+1];
						}
					}
				}else if(osdLen > osLen-1){//传入数据比规则数据长，把结果并入当前数据第一位并进行递归继续下一次计算，直到最后
					endType = 2;
					for(int j=0;j<osdLen;j++){
						if(osd[j] != os[j] && !osd[j].equals(os[j])){
							break;
						}
						if(j == osLen-2){
							end = os[j+1];
						}
					}
					if(end != null){//如果结果不为空，继续并入原数据第一位再次递归
						Object[] newosd = new Object[osdLen-1];
						newosd[0] = osd[0];
						newosd[1] = end;
						for(int j=2;j<osdLen-1;j++){
							newosd[j] = osd[j+1];
						}
						end = getResult(newosd);
						break;
					}
				}
			}
			if(end != null){
				break;
			}
		}
		if(end == null){//尝试使用应用规则
			String name = osd[0]+"_Gz";
			for(int i=0;i<num;i++){
				Object os[] = sjy[i];
				try {
					if(os!=null && os[0].equals(name)){
						Object to = os[1];
						Method m = (Method)os[2];
						Object o = m.invoke(to, osd[1],osd[2]);
						end = o;
						break;
					}
				} catch (Exception e) {
					// TODO Auto-generated catch block
					e.printStackTrace();
				}
			}
			if(endType == 2 && end != null){//如果还有数据需要递归，则继续进行递归
				Object[] newosd = new Object[osdLen-1];
				newosd[0] = osd[0];
				newosd[1] = end;
				for(int j=2;j<osdLen-1;j++){
					newosd[j] = osd[j+1];
				}
				end = getResult(newosd);
			}
		}
		return end;
	}

	private String[] stringToStringArray(String s){
		int sLen = s.length();
		String[] array = new String[sLen];
		for(int i=0;i<sLen;i++){
			char c = s.charAt(i);
			array[i] = c+"";
		}
		return array;
	}
}































